#include <iostream>
#include "Reactor.hpp"
#include "Socket.hpp"
#include "Accepter.hpp"

using namespace NetSockModule;
static const int gbacklog = 32;

void Usage(const std::string &proc)
{
    std::cout << "Usage : \r\n"
              << proc << "  port" << std::endl;
}

void OnConnection(Connection::ptr conn)
{
    std::cout << "get is new link fd :" << conn->SockFd() << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    int port = std::atoi(argv[1]);
    std::unique_ptr<Reactor> svr = std::make_unique<Reactor>();
    // 创建listensock
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->BuildListenSocketMethod(port, gbacklog);
    // 创建连接
    Connection::ptr conn = ConnectionFactory::ListenConnection(listensock->GetSockFd(), EPOLLIN | EPOLLET, svr.get());
    //注册链接到来的回调函数
    conn->RegisterCallback(Accepter::AccepterConnection, nullptr, nullptr);
    svr->AddConnection(conn);
    svr->Loop();
    return 0;
}